package com.hdjk.view.callback;

import android.text.TextUtils;

import com.hdjk.view.entity.UserEntity;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;

/**
 * Created by Admin on 2017/10/13.
 */

public abstract class LoginCallback extends Callback<Msg> {
    @Override
    public Msg parseNetworkResponse(Response response, int id)
            throws IOException {
        Msg msg = new Msg();

        try {
            String result = response.body().string();

            if (!TextUtils.isEmpty(result)) {
                JSONObject rootObject = new JSONObject(result);

                msg.setSuccess(rootObject.getString("code").equals("1"));
                msg.setMsg(rootObject.getString("msg"));

                if (msg.getSuccess()) {
                    JSONObject dataJson = rootObject.getJSONObject("result");
                    UserEntity entity =new UserEntity();
                    if (!dataJson.isNull("Token") && !"null".equals(dataJson.getString("Token"))) {
                        entity.setToken(dataJson.getString("Token"));
                    }
                    if (!dataJson.isNull("LoginName") && !"null".equals(dataJson.getString("LoginName"))) {
                        entity.setLoginName(dataJson.getString("LoginName"));
                    }
                    if (!dataJson.isNull("Id") && !"null".equals(dataJson.getString("Id"))) {
                        entity.setId(dataJson.getString("Id"));
                    }
                    if (!dataJson.isNull("HoldingCode")) {
                        entity.setHoldingCode(dataJson.getInt("HoldingCode"));
                    }
                    if (!dataJson.isNull("md5Pw") && !"null".equals(dataJson.getString("md5Pw"))) {
                        entity.setMd5Pw(dataJson.getString("md5Pw"));
                    }
                    if (!dataJson.isNull("Phone") && !"null".equals(dataJson.getString("Phone"))) {
                        entity.setPhone(dataJson.getString("Phone"));
                    }
                    msg.setData(entity);
                }
            }
        } catch (Exception e) {
        }

        return msg;
    }
}